import { Service, Inject } from "typedi";
import { TopicService } from "../TopicService";
import { UserDao } from "../../dao/UserDao";
import { TopicDao } from "../../dao/TopicDao";
import { DynamicDao } from "../../dao/DynamicDao";
import { TagEntity } from "../../entity/TagEntity";
import { UserEntity } from "../../entity/UserEntity";
import {
  TopicModel,
  SubModel,
  WorkDynamicListModel,
  getIndexTpoic,
} from "../../model/TopicModel";
import { DynamicEntity } from "../../entity/DynamicEntity";
import { utils } from "../../utils/utils";
@Service()
export class TopicServiceImpl implements TopicService {
  @Inject()
  private TopicDao!: TopicDao;
  @Inject()
  private UserDao!: UserDao;
  @Inject()
  private DynamicDao!: DynamicDao;


  public async topicContent(params: TopicModel): Promise<any> {
    let topic: TagEntity = await this.changeTopic(
      await this.TopicDao.getOneTopicDao(params.topic_Id),
      (await this.UserDao.getUserInfoDao(params.user_Id))!
    );
    let DynamicListObject: { string: DynamicEntity[] };

    return await Promise.all(
      JSON.parse(topic.tag_DynamicList).map(async (e: string) => {
        let dynamic: DynamicEntity = await this.DynamicDao.getOneDynamic(
          parseInt(e)
        );
        let userInfo: UserEntity | undefined = await this.UserDao.getUserInfoDao(
          params.user_Id
        );
        JSON.parse(userInfo!.user_UserInfo.user_FollowList).some((e: string) => {
          if (parseInt(e) == dynamic.dynamic_User.user_Id) {
            dynamic.dynamic_User.isFollow = true;
            return true;
          }
        });
        dynamic!.dynamic_User.user_NickName = utils.uncodeUtf16(
          dynamic!.dynamic_User.user_NickName
        );
        JSON.parse(userInfo!.user_UserInfo.user_DynamicCollectionList).some(
          (e: string) => {
            if (parseInt(e) == dynamic.dynamic_Id) {
              dynamic.isCollection = true;
              return true;
            }
          }
        );
        JSON.parse(userInfo!.user_UserInfo.user_DynamicLikeList).some(
          (e: string) => {
            if (parseInt(e) == dynamic.dynamic_Id) {
              dynamic.isLike = true;
              return true;
            }
          }
        );
        dynamic.dynamic_TagList = JSON.parse(dynamic.dynamic_TagList);
        dynamic.dynamic_Info.dynamicInfo_ImgList = JSON.parse(
          dynamic.dynamic_Info.dynamicInfo_ImgList
        );
        dynamic.dynamic_Introduce = utils.uncodeUtf16(dynamic.dynamic_Introduce);
        return dynamic;
      })
    ).then(async (res: any) => {
     
      DynamicListObject = this.workNewDynmicList({
        dynamic_List: res,
        newDynamic_Page: params.newDynamic_Page,
        newDynamic_Count: params.newDynamic_Count,
      });
      return { ...topic, ...DynamicListObject };
    });
  }

  public async changeTopic(
    topic: TagEntity,
    userInfo: UserEntity
  ): Promise<any> {
    JSON.parse(userInfo!.user_UserInfo.user_SubList).some((e: string) => {
      if (parseInt(e) == topic.id) {
        topic.isSub = true;
        return true;
      }
    });
    return topic;
  }

  public async getAllTagClass(): Promise<any> {
    return await this.TopicDao.getAllTagClass();
  }

  public async getIndexAllTopic(params: getIndexTpoic): Promise<any> {
    let Topic_List: TagEntity[] = [];
    let User: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      params.user_Id
    );
    if (params.topic_Index == 0) {
      return await this.getSubTopicList(params, Topic_List, User!);
    } else if (params.topic_Index == 1) {
      Topic_List = await this.TopicDao.getHotTopic();
    } else {
      Topic_List = await this.TopicDao.getIndexAllTpoic(params);
    }
    return await Promise.all(
      Topic_List.map(async (e: TagEntity) => {
        return await this.changeTopic(e, User!);
      })
    ).then((res: any) => {
      return res;
    });
  }

  public async getSubTopicList(
    params: getIndexTpoic,
    Topic_List: TagEntity[],
    User: UserEntity
  ): Promise<any> {
    return await Promise.all(
      JSON.parse(User!.user_UserInfo.user_SubList).map(async (e: string) => {
        return await this.changeTopic(
          await this.TopicDao.getOneTopicDao(parseInt(e)),
          User
        );
      })
    ).then((res: any) => {
      return (Topic_List = res);
    });
  }

  public workNewDynmicList(params: WorkDynamicListModel): any {
    params.dynamic_List.sort((a: DynamicEntity, b: DynamicEntity): number => {
      return b.dynamic_LikeCount - a.dynamic_LikeCount;
    });
    let hotDynamicList: DynamicEntity[] = params.dynamic_List.slice(0, 3);
    params.dynamic_List.splice(1, hotDynamicList.length);
    let newDynamicList: DynamicEntity[] = params.dynamic_List
      .sort((a: DynamicEntity, b: DynamicEntity): number => {
        return b.dynamic_Time.getTime() - a.dynamic_Time.getTime();
      })
      .splice(
        params.newDynamic_Page * params.newDynamic_Count,
        params.newDynamic_Count
      );

    return { hotDynamicList, newDynamicList };
  }

  public async changeSubTopic(params: SubModel): Promise<any> {
    let user: UserEntity | undefined = await this.UserDao.getUserInfoDao(
      params.user_Id
    );
    let user_SubList: string[] = JSON.parse(user!.user_UserInfo.user_SubList);
    if (params.isSub == "true") {
      user_SubList[user_SubList.length] = params.topic_Id.toString();
    } else {
      user_SubList.forEach((element: string, i: number) => {
        if (element == params.topic_Id.toString()) {
          user_SubList.splice(i, 1);
          return;
        }
      });
    }
    await this.TopicDao.changeSubTopic(params.topic_Id, params.isSub);
    return await this.UserDao.setSubListDao(
      user!.user_Id,
      JSON.stringify(user_SubList)
    );
  }
}
